package com.cc.dao.Impl;

import com.cc.dao.BaseDao;
import com.cc.dao.OssDao;
import com.cc.entity.Oss;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OssDaoImpl extends BaseDao implements OssDao {
    @Override
    public List<Oss> selectAll() {
        String sql = "SELECT * FROM `oss`";
        ResultSet resultSet = executeQuery(sql);
        List<Oss> ossList = new ArrayList<>();
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Oss oss = new Oss();
                    oss.setId(resultSet.getInt("id"));
                    oss.setBucketName(resultSet.getString("bucketName"));
                    oss.setType(resultSet.getString("type"));
                    oss.setCapacity(resultSet.getInt("capacity"));
                    oss.setFlow(resultSet.getInt("flow"));
                    oss.setVisits(resultSet.getInt("visits"));
                    oss.setPermission(resultSet.getString("permission"));
                    ossList.add(oss);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }
        return ossList;
    }

    @Override
    public Oss ossInfo(String bucketName) {
        String sql = "SELECT * FROM oss o WHERE o.bucketName=?";
        ResultSet resultSet = executeQuery(sql, bucketName);
        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                    Oss oss = new Oss();
                    oss.setId(resultSet.getInt("id"));
                    oss.setBucketName(resultSet.getString("bucketName"));
                    oss.setType(resultSet.getString("type"));
                    oss.setCapacity(resultSet.getInt("capacity"));
                    oss.setFlow(resultSet.getInt("flow"));
                    oss.setVisits(resultSet.getInt("visits"));
                    oss.setPermission(resultSet.getString("permission"));
                    return oss;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }
        return null;
    }

    @Override
    public int addOss(Oss oss) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" INSERT INTO oss (");
        stringBuilder.append(" oss.bucketName,oss.type,oss.capacity,");
        stringBuilder.append(" oss.flow,oss.visits,oss.permission)");
        stringBuilder.append(" VALUES(?,?,?,?,?,?)");

        return executeUpdate(stringBuilder.toString(),
                oss.getBucketName(),oss.getType(),oss.getCapacity(),
                oss.getFlow(),oss.getVisits(),oss.getPermission());
    }

    @Override
    public int updateOss(String name, String permission) {
        String sql = "UPDATE oss SET permission = ? WHERE id = ?";
        return executeUpdate(sql,permission,name);
    }
}